//
//  SaveTool.m
//  AFNnetworking
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 mjh. All rights reserved.
//

#import "SaveTool.h"

@implementation SaveTool

#pragma mark 判断是否有文件夹
+ (void)isHaveModelFolder{
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *modelFilePath = [sandBoxPath stringByAppendingPathComponent:@"TYTT"];
    //创建管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    //通过管理者调用方法,根据路径判断文件夹是否存在
    if (![manager fileExistsAtPath:modelFilePath]) {
        [manager createDirectoryAtPath:modelFilePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

#pragma mark 判断是否存在
+ (BOOL)isHaveModelInPlist:(id)model type:(saveModel)type{
    NSString *path = [self pathOfType:type];
    NSLog(@"%@",path);
    NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    if (type == SaveTouringModel) {
        JSMTouringModel *object = model;
        for (JSMTouringModel *md in arr) {
            if ([object.touringId isEqualToString:md.touringId]) {
                return NO;
            }
        }
    }else if (type == SaveLeavingModel){
//        //写自己的model
        JSMLeavingHeaderShowCityModel *object = model;
        for (JSMLeavingHeaderShowCityModel *md in arr) {
            if ([object.cityId isEqualToNumber:md.cityId]) {
                return NO;
            }
        }
      
    }else if (type == SaveDreamCitySpecialModel){
        //写自己的model
        JSMDreamCityModel *object = model;
        for (JSMDreamCityModel *md in arr) {
            if ([object.vid isEqualToNumber:md.vid]) {
                return NO;
            }
        }
    }else if (type == SaveDreamCityPrefectureModel){
        //写自己的model
        JSMDreamCityPrecialTrailsDetailModel *object = model;
        for (JSMDreamCityPrecialTrailsDetailModel *md in arr) {
            if ([object.vid isEqualToString:md.vid]) {
                return NO;
            }
        }
    }else if (type == SaveStrategyModel){
        //写自己的model

    }
    return YES;
}
#pragma mark 保存
+ (void)saveModelToPlist:(id)model type:(saveModel)type{
    NSString *path = [self pathOfType:type];
        NSLog(@"%@",path);
    //先反
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    if (arr == nil) {
        //如果为空先进行初始化
        arr = [NSMutableArray array];
    }
    [arr addObject:model];
    //归档
    [NSKeyedArchiver archiveRootObject:arr toFile:path];
}

#pragma mark 取消收藏
+ (void)cancelSaveModelInPlist:(id)model type:(saveModel)type{
    NSString *path = [self pathOfType:type];
    NSLog(@"%@",path);
    //反
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
    if (type == SaveTouringModel) {
        JSMTouringModel *object = model;
        for (JSMTouringModel *md in arr) {
            if ([object.touringId isEqualToString:md.touringId]) {
                [arr removeObject:md];
                break;
            }
        }
    }else if (type == SaveLeavingModel){
        JSMLeavingHeaderShowCityModel *object = model;
        for (JSMLeavingHeaderShowCityModel *md in arr) {
            if ([object.cityId isEqualToNumber:md.cityId]) {
                [arr removeObject:md];
                break;
            }
        }
        
    }else if (type == SaveDreamCitySpecialModel){
        JSMDreamCityModel *object = model;
        for (JSMDreamCityModel *md in arr) {
            if ([object.vid isEqualToNumber:md.vid]) {
                [arr removeObject:md];
                break;
            }
        }
    }else if (type == SaveDreamCityPrefectureModel){
        JSMDreamCityPrecialTrailsDetailModel *object = model;
        for (JSMDreamCityPrecialTrailsDetailModel *md in arr) {
            if ([object.vid isEqualToString:md.vid]) {
                [arr removeObject:md];
                break;
            }
        }
    }else if (type == SaveStrategyModel){
        //写自己的model
        
    }
    
    //归档
    [NSKeyedArchiver archiveRootObject:arr toFile:path];

}
#pragma mark 显示所有收藏
+ (NSMutableArray *)allSaveModelInPlisttype:(saveModel)type{
    NSString *path = [self pathOfType:type];
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    return arr;
}

#pragma mark PLIST
+ (NSString *)pathOfType:(saveModel)type{
    NSString *sandPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path;
    if (type == SaveLeavingModel ) {
        path = [sandPath stringByAppendingPathComponent:@"TYTT/leaving.plist"];
    } else if (type == SaveDreamCitySpecialModel){
        path = [sandPath stringByAppendingPathComponent:@"TYTT/dreamCitySpecial.plist"];
    }else if (type == SaveDreamCityPrefectureModel){
        path = [sandPath stringByAppendingPathComponent:@"TYTT/dreamCityPrecial.plist"];
    }else if (type == SaveTouringModel){
        path = [sandPath stringByAppendingPathComponent:@"TYTT/touring.plist"];
    }else if (type == SaveStrategyModel){
        path = [sandPath stringByAppendingPathComponent:@"TYTT/strategy.plist"];
    }
    
    return path;
}

@end
